package com.bolt.core.view.components;


import com.bolt.common.BoltConstants;
import com.bolt.common.collection.MapUtil;
import com.bolt.common.utils.StrUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Writer;
import java.util.List;

/***
 * 表单元素
 * @author michao
 */
public class FormItem extends ComponentsBase {


    private String label;
    private String type;
    private boolean singleColumn = false;
    private int colspan;
    private String validator;
    private boolean updateHidde;
    private boolean updateOnlyRead = false;
    private boolean combination = false;

    private boolean queryField;

    private String matchType;

    private BindDataModel bindDataModel;

    private List<BindEventModel> bindEventModels;

    public boolean isCombination() {
        return combination;
    }

    public void setCombination(boolean combination) {
        this.combination = combination;
    }

    public boolean isUpdateHidde() {
        return updateHidde;
    }

    public void setUpdateHidde(boolean updateHidde) {
        this.updateHidde = updateHidde;
    }

    public boolean isSingleColumn() {
        return singleColumn;
    }

    public void setSingleColumn(boolean singleColumn) {
        this.singleColumn = singleColumn;
    }

    public FormItem() {
        super();
    }

    @Override
    protected String getDefaultTemplate() {
        return this.type;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getType() {
        return StrUtil.trim(type);
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isUpdateOnlyRead() {
        return updateOnlyRead;
    }

    public void setUpdateOnlyRead(boolean updateOnlyRead) {
        this.updateOnlyRead = updateOnlyRead;
    }

    @Override
    public void end(HttpServletRequest request, HttpServletResponse response, Writer writer) {
        super.end(request, response, writer);
        request.removeAttribute(BoltConstants.FORM_ITME_VALUE_ATTRIBUTE);
    }

    public boolean isHidden() {
        return "hidden".equals(type);
    }

    public boolean isQueryField() {
        return queryField;
    }

    public void setQueryField(boolean queryField) {
        this.queryField = queryField;
    }

    public String getMatchType() {
        return matchType;
    }

    public void setMatchType(String matchType) {
        this.matchType = matchType;
    }

    @Override
    protected void evaluateParams(HttpServletRequest request) {
        super.evaluateParams(request);
        if (updateOnlyRead) {
            addParameter("updateOnlyRead", true);
        }

    }

    public String getValidator() {
        return validator;
    }

    public void setValidator(String validator) {
        this.validator = validator;
    }

    public int getColspan() {
        return colspan;
    }

    public void setColspan(int colspan) {
        this.colspan = colspan;
    }

    public boolean existParameter(String key) {
        return MapUtil.isNotEmpty(this.parameters) && this.parameters.containsKey(key);
    }

    public BindDataModel getBindDataModel() {
        return bindDataModel;
    }

    public void setBindDataModel(BindDataModel bindDataModel) {
        this.bindDataModel = bindDataModel;
    }

    public List<BindEventModel> getBindEventModels() {
        return bindEventModels;
    }

    public void setBindEventModels(List<BindEventModel> bindEventModels) {
        this.bindEventModels = bindEventModels;
    }
}
